<?php
include_once('inc/functions.inc.php');

if(!isEmpty(getParam('operation'))){
    
    switch(getParam('operation')){
        case 'contenido': {
            fntAcademicoMateriasContenido($db);
            break;
        }
        case 'reporte': {
            fntAcademicoMateriasReporte($db);
            break;
        }
        case 's': {
            fntAcademicoMateriasGrabar($db);
            break;
        }
    }
    die();
}

function fntAcademicoMateriasContenido($db){
    //VARIBLES
    $strId = "";
    $strQuery = "";
    $arrInfo = array();
    $strCodigo = "";
    $strNombre = "";
    $strSelected = "";
    //VARIBLES
    
    $strId = isset($_POST["hdnAcademicoMaterias_Id"]) ? $_POST["hdnAcademicoMaterias_Id"] : "";
    if( !empty($strId) ) {
        $strQuery = "SELECT * FROM cllg_academico_materias WHERE ID = '{$strId}'";
        $qTMP = $db->queryToArray($strQuery,true);
        foreach ( $qTMP as $rTMP  ) { 
            $arrInfo = $rTMP;        
        }
        $db->free_result($qTMP);
    }
    
    $strCodigo = isset($arrInfo["CODIGO"]) ? $arrInfo["CODIGO"] : "";
    $strNombre = isset($arrInfo["NOMBRE"]) ? $arrInfo["NOMBRE"] : "";
    $strDuracion = isset($arrInfo["DURACION"]) ? $arrInfo["DURACION"] : "";
    
    $strAnioInicio = isset($arrInfo["ANIO_INICIO"]) ? $arrInfo["ANIO_INICIO"] : "";
    $strAnioFin = isset($arrInfo["ANIO_FIN"]) ? $arrInfo["ANIO_FIN"] : "";
    $strMesInicio = isset($arrInfo["MES_INICIO"]) ? $arrInfo["MES_INICIO"] : "";
    $strMesFin = isset($arrInfo["MES_FIN"]) ? $arrInfo["MES_FIN"] : "";
    $strActivo = isset($arrInfo["ACTIVO"]) ? $arrInfo["ACTIVO"] : "";
    $strfk_nivel = isset($arrInfo["FK_NIVEL"]) ? $arrInfo["FK_NIVEL"] : "";
    
    ?>
    <br>
    <br>
    <div style="width: 150px; height: 30px; float: left;"><b>Codigo</b></div>
    <div style="width: 350px; height: 30px; float: left;"><input type="text" name="txtAcademicoMaterias_codigo" value="<?php print $strCodigo; ?>"></div>
    <div style="width: 150px; height: 30px; float: left;"><b>Nombre</b></div>
    <div style="width: 350px; height: 30px; float: left;"><input type="text" name="txtAcademicoMaterias_nombre" value="<?php print $strNombre; ?>"></div>
    <div style="width: 150px; height: 30px; float: left;"><b>Duración</b></div>
    <div style="width: 350px; height: 30px; float: left;"><input type="text" name="txtAcademicoMaterias_duracion" value="<?php print $strDuracion; ?>"></div>
    <div style="width: 150px; height: 30px; float: left;"><b>Activo</b></div>
    <div style="width: 350px; height: 30px; float: left;">Si&nbsp;<input type="radio" name="radAcademicoMaterias_activo" value="Y" <?php print ($strActivo=="Y")? "checked" : ""; ?>>&nbsp;No<input type="radio" name="radAcademicoMaterias_activo" value="N" <?php print ($strActivo=="N")? "checked" : ""; ?>></div>
    <?php
    
}
function fntAcademicoMateriasReporte($db){

    $strQuery = "SELECT * FROM cllg_academico_materias ORDER BY CODIGO,NOMBRE";
    $qTMP = $db->queryToArray($strQuery,true);
    
    ?>
    <style type="">
    table {
        border-collapse: collapse;
    }
    table td{
        border: 1px solid #AAAAAA;        
    }
    .oddtr {
        background-color:#FFFFFF;
    }
    .eventr {
        background-color:#FFFFFF;
    }
    .trover {
        background-color: #ECEAE0;
    }
    .trclick {
        background-color: #ECEAE0;
    }
    </style>
    <br>
    <br>
    <table width="100%" cellpadding="2" cellspacing="0" border="0">
        <tbody>
            <td width="25%" align="center" style="background-color: #ECEAE0;"><b>Codigo</b></td>
            <td width="25%" style="background-color: #ECEAE0;"><b>Nombre</b></td>
            <td width="25%" style="background-color: #ECEAE0;"><b>Duración</b></td>
            <td width="25%" align="center" style="background-color: #ECEAE0;"><b>Activo</b></td>
        </tbody>
    </table>
    <table width="100%" cellpadding="2" cellspacing="0" border="0" id="tblAcademicoMaterias">   
        <?php    
        
        
            foreach ( $qTMP  as $rTMP ) { 
                ?>
                <tr id="<?php print $rTMP["ID"]; ?>">
                    <td align="center" width="25%"><?php print $rTMP["CODIGO"]; ?></td>
                    <td width="25%"><?php echo $rTMP["NOMBRE"]; ?></td>
                    <td width="25%"><?php print $rTMP["DURACION"]; ?></td>
                    <td width="25%" align="center"><?php print $rTMP["ACTIVO"]; ?></td>
                </tr>        
                <?php
            }
            
        
        $db->free_result($qTMP);
        ?>
    </table>
    <script type="text/javascript">
    //these two line adds the color to each different row
    $("#tblAcademicoMaterias tr:even").addClass("eventr");;
    $("#tblAcademicoMaterias tr:odd").addClass("oddtr");;
    //handle the mouseover , mouseout and click event
    $("#tblAcademicoMaterias tr").mouseover(function() {
        $(this).addClass("trover");
    }).mouseout(function() {
        $(this).removeClass("trover");
    }).click(function() {
        $("input[name='hdnAcademicoMaterias_Id']").val( $(this).attr("id") );
        fntAcademicoMateriasContenido();
    });
    </script>
    <?php
    
}
function fntAcademicoMateriasGrabar($db){

    $strId = isset($_POST["hdnAcademicoMaterias_Id"]) ? $_POST["hdnAcademicoMaterias_Id"] : "";
    $strFields = "";
    $strValues = "";
    
    while( $arrPost = each($_POST) )  {
        $arrExplode = explode( "_", $arrPost["key"]);
        
        if( $arrPost["key"] != "hdnAcademicoMaterias_Id" ){
            if( empty($strId) ) {
                $strFields .= (empty($strFields)) ? "" : ","; 
                $strFields .=  strtoupper( str_ireplace( "-", "_", $arrExplode[1] ) );
                
                $strValues .= empty( $strValues ) ? "" : ",";
                $strValues .= "'{$arrPost["value"]}'";
            }
            else {
                $strFields .= (empty($strFields)) ? "" : ",";
                $strFields .=  strtoupper( str_ireplace( "-", "_", $arrExplode[1] ) ). " = '{$arrPost["value"]}'"; 
                
            }
        }
        
    }
    if( empty($strId) ) {
        $db->query("INSERT INTO cllg_academico_materias ({$strFields}) VALUES ({$strValues})");
    }
    else {
        $db->query("UPDATE cllg_academico_materias SET {$strFields} WHERE ID = '{$strId}'");
    }
    
    
}
?>